package com.wh.myrealm;

import com.wh.dao.UserMapper;
import com.wh.pojo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import java.sql.SQLException;

/**
 * @Auther: HuiEr
 * @Date: 2020/2/27 9:16
 * @Description
 */
public class MyHashRealm extends AuthorizingRealm {

    private UserMapper userMapper = new UserMapper();

    @Override
    public String getName() {
        return "MyHashRealm";
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String)authenticationToken.getPrincipal();
        User user=null;
        try {
            user = userMapper.findUserByName(username);
        } catch (SQLException e) {
            System.out.println("根据用户名查询用户sql执行错误：：："+e.getMessage());
        }

        if (null == user){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.getUserName(),
                user.getPassword(),
                ByteSource.Util.bytes("abc"),
                this.getName());
        return authenticationInfo;
    }
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


}
